package com.live.activity;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.live.common.Constant;
import com.live.task.LiveUser;
import com.live.tools.LiveTools;

public class LiveUserRegistActivity extends LiveBaseActivity {

	private TextView man = null;
	private TextView woman = null;
	private String userSex = Constant.USER_SEX_MAN;
	private String userBirthday = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.user_regist);

		// 设置默认性别
		userSex = Constant.USER_SEX_MAN;

		// 设置性别
		man = (TextView) findViewById(R.id.man);
		man.setOnClickListener(new TextView.OnClickListener() {
			@Override
			public void onClick(View v) {
				userSex = Constant.USER_SEX_MAN;
				man.setBackgroundDrawable(getResources().getDrawable(R.drawable.man_on));
				woman.setBackgroundDrawable(getResources().getDrawable(R.drawable.woman_off));
			}
		});

		woman = (TextView) findViewById(R.id.woman);
		woman.setOnClickListener(new TextView.OnClickListener() {
			@Override
			public void onClick(View v) {
				userSex = Constant.USER_SEX_WOMAN;
				man.setBackgroundDrawable(getResources().getDrawable(R.drawable.man_off));
				woman.setBackgroundDrawable(getResources().getDrawable(R.drawable.woman_on));
			}
		});

		// 单击日期
		ImageView calendar = (ImageView) findViewById(R.id.calendar);
		calendar.setOnClickListener(new ImageView.OnClickListener() {
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		});

		// 用户注册按钮
		Button registBtn = (Button) findViewById(R.id.registBtn);

		registBtn.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				EditText userId = (EditText) findViewById(R.id.userId);
				EditText userPwd = (EditText) findViewById(R.id.userPwd);
				EditText userPwdRp = (EditText) findViewById(R.id.userPwdRp);

				// 用户注册数据合法性验证
				if (!userValidate(v, userId, userPwd, userPwdRp)) {
					return;
				}

				// 取得用户生日
				TextView Birthday = (TextView) findViewById(R.id.userBirthday);
				userBirthday = (String) Birthday.getText();

				String url = Constant.SITE_URL + "live_user.php?locale=" + LiveTools.getLocaleLanguage() + "&";
				url += "userId=" + userId.getText().toString() + "&userPwd=" + userPwd.getText().toString()
						+ "&userPwdRp=" + userPwdRp.getText().toString() + "&userSex=" + userSex + "&userBirthday="
						+ userBirthday + "&type=r";

				user.setUserId(userId.getText().toString());
				user.setUserPwd(userPwd.getText().toString());
				user.setUserSex(userSex);
				user.setUserBirthday(userBirthday);
				user.setUserType("1");

				LiveUser task = new LiveUser(context, url, "注册", LiveUserRegistActivity.this);
				task.execute(user);
			}
		});

		// 注册取消按钮
		Button cancelBtn = (Button) findViewById(R.id.cancelBtn);
		cancelBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
	}

	// 设置要检索的日期
	private DatePickerDialog.OnDateSetListener mSetDateListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int month, int day) {
			Calendar cal = Calendar.getInstance();
			cal.set(year, month, day);
			String date = sdf.format(cal.getTime());

			TextView Birthday = (TextView) findViewById(R.id.userBirthday);
			Birthday.setText(date);

			userBirthday = date;
		}
	};

	protected Dialog onCreateDialog(int id) {
		Calendar c = Calendar.getInstance();
		switch (id) {
		case 0:
			return new DatePickerDialog(this, this.mSetDateListener, c.get(1), c.get(2), c.get(5));
		}
		return null;
	}

	/**
	 * 用户注册数据合法性验证
	 * 
	 * @param v
	 * @param userId
	 * @param userPwd
	 * @param userPwdRp
	 * @return
	 */
	public boolean userValidate(View v, EditText userId, EditText userPwd, EditText userPwdRp) {
		if ("".equals(userId.getText().toString())) {
			Toast.makeText(v.getContext(), "请输入用户名！", Toast.LENGTH_LONG).show();
			return false;
		}
		if (userId.getText().toString().length() > 16) {
			Toast.makeText(v.getContext(), "用户名长度请小于16位！", Toast.LENGTH_LONG).show();
			return false;
		}
		if ("".equals(userPwd.getText().toString())) {
			Toast.makeText(v.getContext(), "请输入密码！", Toast.LENGTH_LONG).show();
			return false;
		}
		if ("".equals(userPwdRp.getText().toString())) {
			Toast.makeText(v.getContext(), "请再次输入密码！", Toast.LENGTH_LONG).show();
			return false;
		}
		if (!userPwdRp.getText().toString().equals(userPwd.getText().toString())) {
			Toast.makeText(v.getContext(), "两次输入的密码不同，请重新输入！", Toast.LENGTH_LONG).show();
			return false;
		}
		if (userPwd.getText().toString().length() > 16) {
			Toast.makeText(v.getContext(), "密码长度请小于16位！", Toast.LENGTH_LONG).show();
			return false;
		}
		return true;
	}
}